/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonInfrastructure.system.managedbean;

import commonInfrastructure.system.entity.SystemParameterEntity;
import commonInfrastructure.system.session.SystemSessionBean;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import scm.rawMaterialPurchasing.session.RawMaterialPurchasingTimerSessionBean;

/**
 *
 * @author ReinforceII
 */
@ManagedBean
@ViewScoped
public class SystemSettingsManagedBean {

    @EJB
    SystemSessionBean systemSessionBean;
    @EJB
    RawMaterialPurchasingTimerSessionBean rawMaterialPurchasingTimerSessionBean;
    private SystemParameterEntity systemParameter;

    /** Creates a new instance of SystemSettingsManagedBean */
    public SystemSettingsManagedBean() {
    }

    public void initView(PhaseEvent event) {
        if (getSystemParameter() == null) {
            setSystemParameter(systemSessionBean.getSystemParameter());
        }
    }

    public void update(ActionEvent event) {
        System.out.println("==============================================Changes saved" + systemParameter.getRawMaterialPurchasingTimer());
        systemSessionBean.saveChanges(systemParameter);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Changes saved.", ""));
    }

    public void startRawMaterialPurchasingTimer(ActionEvent event) {

        rawMaterialPurchasingTimerSessionBean.createTimer();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Timer started.", ""));
    }

    /**
     * @return the systemParameter
     */
    public SystemParameterEntity getSystemParameter() {
        return systemParameter;
    }

    /**
     * @param systemParameter the systemParameter to set
     */
    public void setSystemParameter(SystemParameterEntity systemParameter) {
        this.systemParameter = systemParameter;
    }
}
